package com.im.service.user.smo;

import com.im.service.dto.*;
import com.im.service.nousedao.StaffMapper;
import com.im.service.user.dao.IStaffOperDAO;
import com.im.service.user.dao.IStaffQueryDAO;
import com.im.util.DateUtil;
import com.im.util.JsonUtil;
import com.im.util.MapUtils;
import com.im.util.ReturnsUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.text.ParseException;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jackson.JsonObjectDeserializer;
import org.springframework.stereotype.Service;

import com.im.service.user.dao.IUserOperDAO;

@Service
public class UserOperSMOImpl implements IUserOperSMO{

	@Autowired
	private IUserOperDAO userOperDAO;
	@Autowired
	private IStaffQueryDAO staffQueryDAO;
	@Autowired
	private IStaffOperDAO staffOperDAO;

	@Override
	public void updateStaff(Staff staff) {
		userOperDAO.updateStaff(staff);		
		//Long.parseLong("ss");
	}

	@Override
	public Staff login(Map<String, Object> param) {

		Staff staff = staffQueryDAO.queryStaff(param);
		if(staff!=null){
			if(!param.get("password").toString().equals(staff.getStaffPwd())){
				staff=null;
			}
		}
		return staff;
	}

	@Override
	public Map<String,Object> saveOrUpdateStaff(JSONObject jsonStr) {
		//
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("resultCode","0");
		resultMap.put("resultMsg","操作成功！");

		if(jsonStr.containsKey("staff")){

			String operType = jsonStr.getJSONObject("staff").getString("operType");

			Staff staff =(Staff) JsonUtil.jsonstrToBeanObj(jsonStr.getJSONObject("staff").toString(),Staff.class);

			//判断条件
			if(staff.getName()== null || "".equals(staff.getName())){
				return ReturnsUtil.buildFailMap(resultMap,"用户名不能为空!");
			}
			if(staff.getOrgId()== null || "".equals(staff.getOrgId())){
				return ReturnsUtil.buildFailMap(resultMap,"组织单位不能为空!");
			}

			if("add".equals(operType)){
				//TODO
				staff.setAreaId(8430100L);
				staff.setPosition("暂无");
				staff.setStatusCd(10);

				staff.setStaffId(UUID.randomUUID().toString());
				staffOperDAO.saveStaff(staff);
			}
			if("modify".equals(operType)){
				staffOperDAO.updateStaff(staff);
			}
			if("del".equals(operType)){
				staffOperDAO.deleteStaff(staff);
			}


		}
		if(jsonStr.containsKey("staff2Role")){
			Staff2Role staff2Role =(Staff2Role) JsonUtil.jsonstrToBeanObj(jsonStr.getJSONObject("staff2Role").toString(),Staff2Role.class);
			if(staff2Role.getRoleId() == null){
				return ReturnsUtil.buildFailMap(resultMap,"赋权时员工角色Id不能为空!");
			}
			if(staff2Role.getStaffId() == null){
				return ReturnsUtil.buildFailMap(resultMap,"赋权时员工Id不能为空!");
			}
			if("add".equals(staff2Role.getOperType())){
				staff2Role.setStaffRoleId(UUID.randomUUID().toString());
				staffOperDAO.saveStaff2Role(staff2Role);
			}else{
				staffOperDAO.updateStaff2Role(staff2Role);
			}
		}
		if(jsonStr.containsKey("staffList")){
			String batchOperType = jsonStr.getString("batchOperType");
			List<String> staffList = (List<String>) jsonStr.get("staffList");
				if(null != staffList && staffList.size() > 0){

					for(String staffId : staffList){
						Staff staff = new Staff();
						if("batchdel".equals(batchOperType)){
							staff.setStatusCd(20);
						}
						if("batcheff".equals(batchOperType)){
							staff.setStatusCd(10);
						}
						staff.setStaffId(staffId);
						staffOperDAO.updateStaff(staff);
					}
				}


		}
		if(jsonStr.containsKey("returnList")){
			//返回用户数组
			resultMap.put("result",staffQueryDAO.queryStaffsByCond(jsonStr));
		}else{
			resultMap.put("result","[]");
		}

		return resultMap;
	}

	@Override
	public Map<String, Object> updateStaffPwd(Staff loginedStaff, JSONObject jsonStr) {
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("resultCode","0");
		resultMap.put("resultMsg","操作成功！");

		String operType =  jsonStr.getString("operType");
		if("changePwd".equals(operType)){

			String oldPwd = jsonStr.getString("oldPwd");
			String newPwd = jsonStr.getString("newPwd");

			if(null == oldPwd
					|| "".equals(oldPwd)
					|| !oldPwd.equals(loginedStaff.getStaffPwd())){
				resultMap.put("resultCode","-1");
				resultMap.put("resultMsg","原密码不正确，请重新输入！");
				return resultMap;
			}

			Staff staff = new Staff();
			staff.setStaffId(loginedStaff.getStaffId());
			staff.setStaffPwd(newPwd);
			staffOperDAO.updateStaff(staff);
		}

		if("resetPwd".equals(operType)){
			String staffId = jsonStr.getString("staffId");
			Staff staff = new Staff();
			staff.setStaffId(staffId);
			staff.setStaffPwd("123456");
			staffOperDAO.updateStaff(staff);
		}

		return resultMap;
	}

	@Override
	public Map<String, Object> saveOrUpdateStaffLimit(JSONObject jsonObject) {
		//
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("resultCode","0");
		resultMap.put("resultMsg","操作成功！");
		JSONArray addlist = jsonObject.getJSONArray("addlist");
		for(int i=0; i<addlist.size(); i++){
			JSONObject jo = addlist.getJSONObject(i);
			jo.remove("limitValueName");
			jo.remove("checkedFlag");
			StaffLimitRange staffLimitRange = (StaffLimitRange) JsonUtil.jsonstrToBeanObj(jo.toString(),StaffLimitRange.class);

			if("-9999".equals(staffLimitRange.getLimitTypeId())){
				staffLimitRange.setStartDt(DateUtil.getFormatTimeString(new Date(),DateUtil.DATE_FORMATE_STRING_B));
				staffLimitRange.setLimitValue("-9999");
				staffLimitRange.setEndDt("3000-01-01");
			}
			staffLimitRange.setLimitRangeId(UUID.randomUUID().toString());
			staffLimitRange.setStatusCd("10");
			staffOperDAO.saveStaffLimitRange(staffLimitRange);
		}
		JSONArray dellist = jsonObject.getJSONArray("dellist");
		for(int i=0; i<dellist.size(); i++){
			JSONObject jo = dellist.getJSONObject(i);
			jo.remove("limitValueName");
			jo.remove("checkedFlag");
			StaffLimitRange staffLimitRange = (StaffLimitRange) JsonUtil.jsonstrToBeanObj(jo.toString(),StaffLimitRange.class);
			try {
				staffLimitRange.setStartDt(DateUtil.getFormatTimeString(DateUtil.getDateFromString(staffLimitRange.getStartDt(),DateUtil.DATE_FORMATE_STRING_A),DateUtil.DATE_FORMATE_STRING_B));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			try {
				staffLimitRange.setEndDt(DateUtil.getFormatTimeString(DateUtil.getDateFromString(staffLimitRange.getEndDt(),DateUtil.DATE_FORMATE_STRING_A),DateUtil.DATE_FORMATE_STRING_B));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			staffLimitRange.setStatusCd("20");
			staffOperDAO.updateStaffLimitRange(staffLimitRange);
		}
		JSONArray modlist = jsonObject.getJSONArray("modlist");
		for(int i=0; i<modlist.size(); i++){
			JSONObject jo = modlist.getJSONObject(i);
			jo.remove("limitValueName");
			jo.remove("checkedFlag");
			StaffLimitRange staffLimitRange = (StaffLimitRange) JsonUtil.jsonstrToBeanObj(jo.toString(),StaffLimitRange.class);
			staffOperDAO.updateStaffLimitRange(staffLimitRange);
		}

		return resultMap;
	}

	@Override
	public Map<String, Object> saveOrUpdateOrg(JSONObject jsonStr) {
		//
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("resultCode","0");
		resultMap.put("resultMsg","操作成功！");

		if(jsonStr.containsKey("org")){

			String operType = jsonStr.getJSONObject("org").getString("operType");

			Orgarization orgarization =(Orgarization) JsonUtil.jsonstrToBeanObj(jsonStr.getJSONObject("org").toString(),Orgarization.class);

			//判断条件
			if("add".equals(operType)){
				orgarization.setOrgId(UUID.randomUUID().toString());
				orgarization.setStatusCd(10);
				orgarization.setOrgCode("11");
				staffOperDAO.saveOrg(orgarization);
			}
			if("modify".equals(operType)){
				staffOperDAO.updateOrg(orgarization);
			}
			if("del".equals(operType)){
				orgarization.setStatusCd(20);
				staffOperDAO.updateOrg(orgarization);
			}


		}

		if(jsonStr.containsKey("orgList")){
			String batchOperType = jsonStr.getString("batchOperType");
			List<String> orgList = (List<String>) jsonStr.get("orgList");
				if(null != orgList && orgList.size() > 0){

                for(String orgId : orgList){
                    Orgarization orgarization = new Orgarization();
                    if("batchdel".equals(batchOperType)){
                        orgarization.setStatusCd(20);
                    }
                    if("batcheff".equals(batchOperType)){
                        orgarization.setStatusCd(10);
                    }
                    orgarization.setOrgId(orgId);
                    staffOperDAO.updateOrg(orgarization);
                }
				}

		}
		return resultMap;
	}
	@Override
	public Map<String, Object> saveOrUpdateRole(JSONObject jsonStr) {
		//
		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("resultCode","0");
		resultMap.put("resultMsg","操作成功！");

		if(jsonStr.containsKey("role")){

			String operType = jsonStr.getJSONObject("role").getString("operType");

			Role role =(Role) JsonUtil.jsonstrToBeanObj(jsonStr.getJSONObject("role").toString(),Role.class);

			//判断条件
			if("add".equals(operType)){
				role.setRoleId(UUID.randomUUID().toString());
				role.setStatusCd(10);
				staffOperDAO.saveRole(role);
			}
			if("modify".equals(operType)){
				if(role.getRoleId() == null || "".equals(role.getRoleId())){
					resultMap.put("resultCode","-1");
					resultMap.put("resultMsg","未定义需要修改的员工！");
					return resultMap;
				}

				staffOperDAO.updateRole(role);
			}
			if("del".equals(operType)){
				if(role.getRoleId() == null || "".equals(role.getRoleId())){
					resultMap.put("resultCode","-1");
					resultMap.put("resultMsg","未定义需要修改的员工！");
					return resultMap;
				}

				role.setStatusCd(20);
				staffOperDAO.updateRole(role);

			}
		}
		if(jsonStr.containsKey("funcMenus")){
			JSONArray jsonArray = jsonStr.getJSONArray("funcMenus");
			if(!jsonArray.isEmpty()){
				for(int i=0; i< jsonArray.size();i++){
					Role2FuncMenu role2FuncMenu =(Role2FuncMenu) JsonUtil.jsonstrToBeanObj(jsonArray.get(i).toString(),Role2FuncMenu.class);

					if("add".equals(role2FuncMenu.getOperType())){
						role2FuncMenu.setDispOrder(Short.parseShort("0"));
						role2FuncMenu.setRoleMenuId(UUID.randomUUID().toString());
						role2FuncMenu.setStatusCd(10);
						staffOperDAO.saveRole2FuncMenu(role2FuncMenu);

					}
					if("del".equals(role2FuncMenu.getOperType())){
						role2FuncMenu.setStatusCd(20);
						staffOperDAO.updateRole2FuncMenu(role2FuncMenu);
					}
				}

			}

		}
        if(jsonStr.containsKey("roleList")){
            String batchOperType = jsonStr.getString("batchOperType");
            List<String> roleList = (List<String>) jsonStr.get("roleList");
            if(null != roleList && roleList.size() > 0){

                for(String roleId : roleList){
                    Role role = new Role();
                    if("batchdel".equals(batchOperType)){
                        role.setStatusCd(20);
                    }
                    if("batcheff".equals(batchOperType)){
                        role.setStatusCd(10);
                    }
                    role.setRoleId(roleId);
                    staffOperDAO.updateRole(role);
                }
            }

        }
		return resultMap;
	}
}
